package club.wx3609.images.controller;

import club.wx3609.images.entity.User;
import club.wx3609.images.interfaces.Constant;
import club.wx3609.images.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class LoginController {

    @Resource
    private UserService userService;

    @RequestMapping({"/","/login"})
    public String loginPage(){
        return "Login";
    }

    @ResponseBody
    @GetMapping("/registerUserNumber")
    public Object registerNumber(HttpServletRequest request){
        Object result=request.getSession().getAttribute("registerUserNumber");
        return result;
    }

    @PostMapping("/Index")
    public String toIndex(String userName, String userPassword, Map<String,Object> map,HttpServletRequest request){
        User user=userService.loginService(userName,userPassword);
        if(user!=null){

            if((Constant.registerState0).equals(user.getRegisterState())){
                map.put("error",Constant.registerState0);
            }else if((Constant.registerState2).equals(user.getRegisterState())){
                map.put("error",Constant.registerState2);
            }else if((Constant.registerState3).equals(user.getRegisterState())){
                map.put("error",Constant.registerState3);
            }else {
                request.getSession().setAttribute("User",user);
                map.put("User", user);
                if(user.getRegisterState().equals("管理员")) {
                    return "ManagementIndex";
                }
                return "Index";
            }
        }else {
            map.put("error", Constant.loginState0);
        }
        return "Login";
    }
}
